package com.tarena.lbs.content.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.pojo.param.ArticleActionParam;
import com.tarena.lbs.content.pojo.param.ArticleContentParam;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleActionPageResultVO;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import com.tarena.lbs.content.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * 文章业务模块接口
 */
@Slf4j
@RestController
@Tag(name = "文章相关功能接口")
public class ArticleController {


    @Autowired
    private ArticleService articleService;
    @Operation(summary = "文章分页列表查询")
    @GetMapping("/admin/content/article/list")
    public Result<PageResult<ArticleVO>> pageList(ArticleQuery articleQuery)
        throws BusinessException{
        log.debug("文章分页列表查询业务处理,入参:{}",articleQuery);
        PageResult<ArticleVO> pageResult = articleService.pageList(articleQuery);
        log.debug("文章分页列表查询业务处理,出参:{}",pageResult);
        return new Result<>(pageResult);
    }


    @Operation(summary = "文章保存")
    @PostMapping("/admin/content/article/insert")
    public Result<Void> addArticle(ArticleContentParam param)
        throws BusinessException{
        log.debug("文章保存业务处理,入参:{}",param);
        articleService.addArticle(param);
        return Result.success();
    }

    @Operation(summary = "文章状态修改")
    @PostMapping("/admin/content/article/update")
    public Result<ArticleVO> updateArticle(ArticleContentParam param)
        throws BusinessException{
        log.debug("文章状态修改业务处理,入参:{}",param);
         ArticleVO ArticleVO = articleService.updateArticle(param);
        return new Result<>(ArticleVO);
    }


    @Operation(summary = "文章标签集合")
    @GetMapping("/admin/content/article/getArticleLabel")
    public Result<Set<String>> articleLabels(ArticleQuery articleQuery) throws BusinessException {
        log.debug("文章标签集合业务处理,入参:{}",articleQuery);
        Set<String> labels = articleService.articleLabels(articleQuery);
        log.debug("文章标签集合业务处理,出参:{}",labels);
        return new Result<>(labels);
    }


    @Operation(summary = "文章详情")
    @GetMapping("/admin/content/article/get")
    public Result<ArticleVO> articleDetail(String id)throws BusinessException{
        log.debug("文章详情业务处理,入参:{}",id);
        ArticleVO articleVO = articleService.detail(id);
        log.debug("文章详情业务处理,出参:{}",articleVO);
        return new Result<>(articleVO);
    }


    @Operation(summary = "用户行为记录")
    @PostMapping("/admin/content/article/behavior")
    public Result<Void> articleBehavior(ArticleActionParam param)
        throws BusinessException{
        log.debug("用户行为记录业务处理,入参:{}",param);
        articleService.articleBehavior(param);
        return Result.success();
    }


    @Operation(summary = "查询已经点赞 收藏 访问过的文章")
    @GetMapping("admin/content/article/getBehaviorList")
    public Result<ArticleActionPageResultVO> getBehaviorLists(ArticleQuery query)
        throws BusinessException{
        log.debug("查询已经点赞 收藏 访问过的文章业务处理,入参:{}",query);
        ArticleActionPageResultVO vo = articleService.getBehaviorLists(query);
        return new Result<>(vo);
    }
    @Operation(summary = "文章删除")
    @PostMapping("/admin/content/article/delete")
    public Result<Void> deleteArticle(String id)throws BusinessException{
        log.debug("文章删除业务处理,入参:{}",id);
        articleService.deleteArticle(id);
        return Result.success();
    }


}
